{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KServe Pipeline samples\n",
    "\n",
    "This notebook assumes your cluster has KServe >= v0.7.0 installed which supports the v1beta1 API."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install the necessary kfp library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install kfp --upgrade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import kfp.compiler as compiler\n",
    "import kfp.dsl as dsl\n",
    "import kfp\n",
    "from kfp import components\n",
    "\n",
    "# Create kfp client\n",
    "# Note: Add the KubeFlow Pipeline endpoint below if the client is not running on the same cluster.\n",
    "# Example: kfp.Client('http://192.168.1.27:31380/pipeline')\n",
    "client = kfp.Client()\n",
    "EXPERIMENT_NAME = \"KServe Experiments\"\n",
    "experiment = client.create_experiment(name=EXPERIMENT_NAME, namespace=\"anonymous\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TensorFlow example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# kfserving_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/master/components/kubeflow/kfserving/component.yaml')\n",
    "kserve_op = components.load_component_from_url(\n",
    "    \"https://raw.githubusercontent.com/kubeflow/pipelines/master/components/kserve/component.yaml\"\n",
    ")\n",
    "\n",
    "\n",
    "@dsl.pipeline(name=\"KServe pipeline\", description=\"A pipeline for KServe.\")\n",
    "def kservePipeline(\n",
    "    action=\"apply\",\n",
    "    model_name=\"tensorflow-sample\",\n",
    "    model_uri=\"gs://kfserving-examples/models/tensorflow/flowers\",\n",
    "    namespace=\"anonymous\",\n",
    "    framework=\"tensorflow\",\n",
    "):\n",
    "\n",
    "    kserve = kserve_op(\n",
    "        action=action,\n",
    "        model_name=model_name,\n",
    "        model_uri=model_uri,\n",
    "        namespace=namespace,\n",
    "        framework=framework,\n",
    "    ).set_image_pull_policy(\"Always\")\n",
    "\n",
    "\n",
    "# Compile pipeline\n",
    "compiler.Compiler().compile(kservePipeline, \"tf-flower.tar.gz\")\n",
    "\n",
    "# Execute pipeline\n",
    "run = client.run_pipeline(experiment.id, \"tf-flower\", \"tf-flower.tar.gz\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom model example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# kfserving_op = components.load_component_from_url('https://raw.githubusercontent.com/kubeflow/pipelines/master/components/kubeflow/kfserving/component.yaml')\n",
    "kserve_op = components.load_component_from_url(\n",
    "    \"https://raw.githubusercontent.com/kubeflow/pipelines/master/components/kserve/component.yaml\"\n",
    ")\n",
    "\n",
    "\n",
    "@dsl.pipeline(name=\"KServe pipeline\", description=\"A pipeline for KServe.\")\n",
    "def kservePipeline(\n",
    "    action=\"apply\",\n",
    "    model_name=\"max-image-segmenter\",\n",
    "    namespace=\"anonymous\",\n",
    "    custom_model_spec='{\"name\": \"image-segmenter\", \"image\": \"codait/max-image-segmenter:latest\", \"port\": \"5000\"}',\n",
    "):\n",
    "\n",
    "    kserve = kserve_op(\n",
    "        action=action,\n",
    "        model_name=model_name,\n",
    "        namespace=namespace,\n",
    "        custom_model_spec=custom_model_spec,\n",
    "    ).set_image_pull_policy(\"Always\")\n",
    "\n",
    "\n",
    "# Compile pipeline\n",
    "compiler.Compiler().compile(kservePipeline, \"custom.tar.gz\")\n",
    "\n",
    "# Execute pipeline\n",
    "run = client.run_pipeline(experiment.id, \"custom-model\", \"custom.tar.gz\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
